from typing import Type
from pip import main
import  requests 

HOST = 'http://124.223.33.41:7081'
def login(inData):
    url = f'{HOST}/api/mgr/loginReq'
    payload = inData 
    resp = requests.post(url,data=payload)
    # cookiess = resp.headers['Set-Cookie'].split(";")[0]
    
    
    return resp.cookies   #  返回cookie


    #  -------------业务代码-----------------

class Lesson:
    def __init__(self,inCookies):
        self.cookie = inCookies

    # 列出课程
    #http://124.223.33.41:7081/api/mgr/sq_mgr/?action=list_course&pagenum=1&pagesize=20
    def list_lesson(self,inData):
        url = f'{HOST}/api/mgr/sq_mgr/' 
        payload = inData 
        resp = requests.get(url,params=payload,cookies=self.cookie)  # cookie 一定是cookies

        # {"retcode": 302, "reason": "\u672a\u767b\u5f55", "redirect": "/mgr/login/login.html"}  原因有乱码
        resp.encoding = 'unicode_escape'   # 响应编码处理
        return resp.text
    # 增加课程
    def add_lesson(inCookies):
        pass


if __name__ == '__main__': 

    # 1 -- 登录操作
    cookie  = login({'username':'auto','password':'sdfsdfsdf'})
    print(type(cookie))   # sessionid 值
    print(cookie)
    #2 -列出课程  
    testData = {"action":"list_course","pagenum":1,"pagesize":20}  # 参数
    # print(type(testData))
    res = Lesson(cookie).list_lesson(testData)
    print(res)
    # print(res)


  


